package com.chuangjiangx.member.coupon.ddd.domain.service;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.commons.wx.card.WxCardUtils;
import com.chuangjiangx.commons.wx.card.model.DecryptCodeResp;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrAccountRepository;
import com.chuangjiangx.member.basic.ddd.domain.model.Member;
import com.chuangjiangx.member.basic.ddd.domain.model.MemberRepository;
import com.chuangjiangx.member.coupon.ddd.application.result.MbrActivateWxCardResult;
import com.chuangjiangx.member.coupon.ddd.domain.model.ActivateWxCard;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrWxCard;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrWxCardRepository;
import com.chuangjiangx.member.basic.ddd.domain.service.MemberRedisDomainService;
import com.chuangjiangx.member.coupon.ddd.domain.model.SwicthEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import weixin.popular.api.CardAPI;
import weixin.popular.bean.card.membercard.activate.ActivateWxCardResult;

/**
 * @Auther: YZJ
 * @Date: 2018/9/4 18:11
 * @Description:激活微信会员卡
 */
@Service
public class ActivateWxCardDomainService {
    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private com.chuangjiangx.member.basic.ddd.domain.model.MbrUserMappingRepository MbrUserMappingRepository;
    @Autowired
    private MbrAccountRepository mbrAccountRepository;
    @Autowired
    private MbrWxCardRepository mbrWxCardRepository;
    @Autowired
    private MemberRedisDomainService memberRedisDomainService;
    @Transactional
    public MbrActivateWxCardResult activateWxCard(ActivateWxCard activateWxCard){
        MbrWxCard mbrWxCard = mbrWxCardRepository.fromWxCardId(activateWxCard.getCardId());
        Assert.notNull(mbrWxCard,"【会员卡激活】未找到微信会员卡信息");
        //accessToken获取
        String accessToken = memberRedisDomainService.getMerchantAccessToken(mbrWxCard.getMerchantId().getId());
        //查询微信会员卡信息
        DecryptCodeResp decryptCodeResp = WxCardUtils.decryptCode(accessToken,activateWxCard.getEncryptCode());
        String code;
        if(decryptCodeResp.isSuccess()){
            code = decryptCodeResp.getCode();
        }else{
            throw new BaseException("","【会员卡激活】code解码失败");
        }
        Member member = memberRepository.fromMemberCardNum(code);
        Assert.notNull(mbrWxCard,"【会员卡激活】未查到会员信息");

        //微信请求封装
        weixin.popular.bean.card.membercard.activate.ActivateWxCard wxCard = new weixin.popular.bean.card.membercard.activate.ActivateWxCard();
        wxCard.setMemberShipNumber(code);
        wxCard.setCode(code);
        wxCard.setCardId(mbrWxCard.getWxCardId());
        ActivateWxCardResult activateWxCardResult = CardAPI.activateWxCard(accessToken,wxCard);
        if (activateWxCardResult.isSuccess()){
            //更新会员同步
            member.syncWxStatus(SwicthEnum.ON);
        }else {
            member.syncWxStatus(SwicthEnum.OFF);
        }
        memberRepository.update(member);
        return new MbrActivateWxCardResult(code,mbrWxCard.getMerchantId().getId());
    }
}
